package com.zhipu.balance.ui.vm.home.calculate.dami

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordDaMi
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 大米黄粒米
 */
class DaMiYellowGrainViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 大米实体
     */
    var mRecordDaMi : RecordDaMi? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 黄粒米-试样质量
     */
    var yellowGrainRiceSampleQuality = ObservableField("")


    /**
     * 黄粒米质量
     */
    var yellowGrainRiceQuality =  ObservableField("")

    /**
     * 黄粒米率
     */
    var yellowGrainRiceResult =  ObservableField("")



    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordDaMi?.yellowGrainRiceRecorder)) {
            //黄粒米-试样质量
            yellowGrainRiceSampleQuality.set(mRecordDaMi?.yellowGrainRiceSampleQuality.toString())
            //黄粒米质量
            yellowGrainRiceQuality.set(mRecordDaMi?.yellowGrainRiceQuality.toString())
            //黄粒米率
            yellowGrainRiceResult.set(mRecordDaMi?.yellowGrainRiceResult.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //黄粒米-试样质量
        yellowGrainRiceSampleQuality.set("")
        //黄粒米质量
        yellowGrainRiceQuality.set("")
        //黄粒米率
        yellowGrainRiceResult.set("")
    }






    /**
     * 试样质量
     */
    fun BulkQuality(){
        yellowGrainRiceSampleQuality.set(weighing)
        mRecordDaMi?.yellowGrainRiceSampleQuality = yellowGrainRiceSampleQuality.get()?.toDouble()
    }

    /**
     * 黄粒米质量
     */
    fun YellowGrainRiceQuality(){
        yellowGrainRiceQuality.set(weighing)
        mRecordDaMi?.yellowGrainRiceQuality = yellowGrainRiceQuality.get()?.toDouble()
    }


    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算黄粒米率
         */
        var mYellowGrainRiceResult: Double = CommonUtils.formatDouble(mRecordDaMi!!.yellowGrainRiceQuality / mRecordDaMi!!.yellowGrainRiceSampleQuality * 100)
        mRecordDaMi?.yellowGrainRiceResult = mYellowGrainRiceResult
        yellowGrainRiceResult.set(mYellowGrainRiceResult.toString())



        /**
         * 黄粒米记录员
         */
        mRecordDaMi!!.yellowGrainRiceRecorder = mPersonnel!!.name


        //黄粒米-试样质量
        if(mRecordDaMi?.yellowGrainRiceSampleQuality == 0.00){
            yellowGrainRiceSampleQuality.set("0.00")
        }
        //黄粒米质量
        if(mRecordDaMi?.yellowGrainRiceQuality == 0.00){
            yellowGrainRiceQuality.set("0.00")
        }
    }
}